{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8cd0ddae-03e3-4c0e-8d21-b8feda9c7287",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T19:56:01.769037Z",
     "iopub.status.busy": "2022-04-15T19:56:01.768787Z",
     "iopub.status.idle": "2022-04-15T19:56:01.772265Z",
     "shell.execute_reply": "2022-04-15T19:56:01.771466Z",
     "shell.execute_reply.started": "2022-04-15T19:56:01.769011Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 导入包\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc733e8e-fde2-4f6a-b17e-0a4cafb91f00",
   "metadata": {},
   "source": [
    "## 小结\n",
    "1. DataFrame:  表格型的数据结构\n",
    "    1. 包含一组有序的列\n",
    "    2. 每一列的值类型可以不同\n",
    "    3. 既有行索引，也有列索引\n",
    "2. 创建方法\n",
    "    1. 通过dict创建\n",
    "    2. 通过list创建\n",
    "3. 基本用法\n",
    "    1. T转置\n",
    "    2. 通过列索引获取列数据\n",
    "    3. 增加列数据\n",
    "    4. 删除列数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "816e61dd-faa1-4c92-969b-46eb03d35f39",
   "metadata": {},
   "source": [
    "## DataFrame构建"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "053bd275-486b-4ad7-894a-ccb44ddeebe8",
   "metadata": {},
   "source": [
    "### 用字典构造DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ec6e5300-d2c7-4268-b063-f7a380c9a784",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T20:07:43.591484Z",
     "iopub.status.busy": "2022-04-15T20:07:43.591229Z",
     "iopub.status.idle": "2022-04-15T20:07:43.609403Z",
     "shell.execute_reply": "2022-04-15T20:07:43.608564Z",
     "shell.execute_reply.started": "2022-04-15T20:07:43.591456Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>9</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   a   b   c\n",
       "0  1   6  11\n",
       "1  2   7  12\n",
       "2  3   8  13\n",
       "3  4   9  14\n",
       "4  5  10  15"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1. 数组、列表或元组构成的字典构造DataFrame\n",
    "# 构造字典\n",
    "data = {\"a\": [1, 2, 3, 4, 5], \"b\": (6, 7, 8, 9, 10), \"c\": np.arange(11, 16)}\n",
    "# 构造DataFrame\n",
    "df = pd.DataFrame(data)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bf95d744-47cc-4329-aa79-c4b21fff30fc",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T20:15:11.728000Z",
     "iopub.status.busy": "2022-04-15T20:15:11.727733Z",
     "iopub.status.idle": "2022-04-15T20:15:11.732744Z",
     "shell.execute_reply": "2022-04-15T20:15:11.732158Z",
     "shell.execute_reply.started": "2022-04-15T20:15:11.727975Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RangeIndex(start=0, stop=5, step=1)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# index属性查看df行索引\n",
    "df.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "21f89c5b-b0a1-42c7-86fb-6684f4704eda",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T20:19:58.959912Z",
     "iopub.status.busy": "2022-04-15T20:19:58.959664Z",
     "iopub.status.idle": "2022-04-15T20:19:58.965146Z",
     "shell.execute_reply": "2022-04-15T20:19:58.964199Z",
     "shell.execute_reply.started": "2022-04-15T20:19:58.959887Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['a', 'b', 'c'], dtype='object')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# columns属性查看df列索引\n",
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "33e732cf-8351-4df6-b9ff-2bf61b7bfcfa",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T20:24:48.317034Z",
     "iopub.status.busy": "2022-04-15T20:24:48.316781Z",
     "iopub.status.idle": "2022-04-15T20:24:48.322052Z",
     "shell.execute_reply": "2022-04-15T20:24:48.321255Z",
     "shell.execute_reply.started": "2022-04-15T20:24:48.317007Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  6, 11],\n",
       "       [ 2,  7, 12],\n",
       "       [ 3,  8, 13],\n",
       "       [ 4,  9, 14],\n",
       "       [ 5, 10, 15]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# values属性查看df列索引\n",
    "df.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "526a46ac-75aa-45fa-af27-42dbaea8ae5f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T20:33:05.014145Z",
     "iopub.status.busy": "2022-04-15T20:33:05.013887Z",
     "iopub.status.idle": "2022-04-15T20:33:05.022729Z",
     "shell.execute_reply": "2022-04-15T20:33:05.022096Z",
     "shell.execute_reply.started": "2022-04-15T20:33:05.014120Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>4</td>\n",
       "      <td>9</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>5</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   a   b   c\n",
       "A  1   6  11\n",
       "B  2   7  12\n",
       "C  3   8  13\n",
       "D  4   9  14\n",
       "E  5  10  15"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 构造DataFrame，指定行索引\n",
    "df2 = pd.DataFrame(data, index=[\"A\", \"B\", \"C\", \"D\", \"E\"])\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3b6db8d4-77c8-4efa-94ec-df6816d9618b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T20:39:20.312061Z",
     "iopub.status.busy": "2022-04-15T20:39:20.310542Z",
     "iopub.status.idle": "2022-04-15T20:39:20.343748Z",
     "shell.execute_reply": "2022-04-15T20:39:20.341285Z",
     "shell.execute_reply.started": "2022-04-15T20:39:20.311987Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>11</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>12</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "      <td>13</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>4</td>\n",
       "      <td>9</td>\n",
       "      <td>14</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>5</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   a   b   c    d\n",
       "A  1   6  11  NaN\n",
       "B  2   7  12  NaN\n",
       "C  3   8  13  NaN\n",
       "D  4   9  14  NaN\n",
       "E  5  10  15  NaN"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 构造DataFrame，指定列索引\n",
    "df3 = pd.DataFrame(data, index=[\"A\", \"B\", \"C\", \"D\", \"E\"], columns=[\"a\", \"b\", \"c\", \"d\"])\n",
    "df3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "31555d85-4b7a-4e98-9e07-e0a883f47575",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-16T03:59:49.640295Z",
     "iopub.status.busy": "2022-04-16T03:59:49.640008Z",
     "iopub.status.idle": "2022-04-16T03:59:49.651014Z",
     "shell.execute_reply": "2022-04-16T03:59:49.650418Z",
     "shell.execute_reply.started": "2022-04-16T03:59:49.640268Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>5.0</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>6.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>7.0</td>\n",
       "      <td>11.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   a    b     c\n",
       "0  0  4.0   8.0\n",
       "1  1  5.0   9.0\n",
       "2  2  6.0  10.0\n",
       "3  3  7.0  11.0\n",
       "4  4  NaN   NaN"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2. Series构成的字典构造DataFrame\n",
    "# 构造字典\n",
    "data2 = {\n",
    "    \"a\": pd.Series(np.arange(5)),\n",
    "    \"b\": pd.Series(np.arange(4, 8)),\n",
    "    \"c\": pd.Series(np.arange(8, 12)),\n",
    "}\n",
    "# 构造DataFrame\n",
    "df4 = pd.DataFrame(data2)\n",
    "df4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c5bbffa-8e1a-4130-a0b6-84c9d77bf853",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 简化版写法\n",
    "df4 = pd.DataFrame(\n",
    "    {\n",
    "        \"a\": pd.Series(np.arange(4)),\n",
    "        \"b\": pd.Series(np.arange(4, 8)),\n",
    "        \"c\": pd.Series(np.arange(8, 12)),\n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "60eb9810-b52c-4f8b-abfb-e338be02016c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-16T06:51:06.385522Z",
     "iopub.status.busy": "2022-04-16T06:51:06.385271Z",
     "iopub.status.idle": "2022-04-16T06:51:06.395116Z",
     "shell.execute_reply": "2022-04-16T06:51:06.394324Z",
     "shell.execute_reply.started": "2022-04-16T06:51:06.385494Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>手机</th>\n",
       "      <td>4999</td>\n",
       "      <td>5999</td>\n",
       "      <td>6999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>平板</th>\n",
       "      <td>2799</td>\n",
       "      <td>3799</td>\n",
       "      <td>4799</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>笔记本</th>\n",
       "      <td>4599</td>\n",
       "      <td>5299</td>\n",
       "      <td>6499</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        a     b     c\n",
       "手机   4999  5999  6999\n",
       "平板   2799  3799  4799\n",
       "笔记本  4599  5299  6499"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3. 字典构成的字典构造DataFrame\n",
    "# 构造字典（字典嵌套）\n",
    "data3 = {\n",
    "    \"a\": {\"手机\": 4999, \"平板\": 2799, \"笔记本\": 4599},\n",
    "    \"b\": {\"手机\": 5999, \"平板\": 3799, \"笔记本\": 5299},\n",
    "    \"c\": {\"手机\": 6999, \"平板\": 4799, \"笔记本\": 6499},\n",
    "}\n",
    "# 构造DataFrame\n",
    "df5 = pd.DataFrame(data3)\n",
    "df5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a96b9725-a810-462f-bcaa-1a04fd4b7bed",
   "metadata": {},
   "source": [
    "### 用列表构造DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "aae4ad59-455f-415b-8f31-641c4cfd4e0e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-16T07:11:28.787221Z",
     "iopub.status.busy": "2022-04-16T07:11:28.786965Z",
     "iopub.status.idle": "2022-04-16T07:11:28.794823Z",
     "shell.execute_reply": "2022-04-16T07:11:28.794122Z",
     "shell.execute_reply.started": "2022-04-16T07:11:28.787195Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0   1   2\n",
       "0  0   1   2\n",
       "1  3   4   5\n",
       "2  6   7   8\n",
       "3  9  10  11"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1. 二维ndarray构造DataFrame\n",
    "# 构造二维数组\n",
    "arr = np.arange(12).reshape(4, 3)\n",
    "# 构造DataFrame\n",
    "df6 = pd.DataFrame(arr)\n",
    "df6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "7a2e41c9-4aa3-4356-8573-aa73261d33c1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-16T07:23:42.553333Z",
     "iopub.status.busy": "2022-04-16T07:23:42.553084Z",
     "iopub.status.idle": "2022-04-16T07:23:42.563608Z",
     "shell.execute_reply": "2022-04-16T07:23:42.562955Z",
     "shell.execute_reply.started": "2022-04-16T07:23:42.553308Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>手机</th>\n",
       "      <th>平板</th>\n",
       "      <th>笔记本</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4999</td>\n",
       "      <td>2799</td>\n",
       "      <td>4599</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5999</td>\n",
       "      <td>3799</td>\n",
       "      <td>5299</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6999</td>\n",
       "      <td>4799</td>\n",
       "      <td>6499</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     手机    平板   笔记本\n",
       "0  4999  2799  4599\n",
       "1  5999  3799  5299\n",
       "2  6999  4799  6499"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2. 字典构成的列表构造DataFrame\n",
    "# 构造列表\n",
    "list1 = [\n",
    "    {\"手机\": 4999, \"平板\": 2799, \"笔记本\": 4599},\n",
    "    {\"手机\": 5999, \"平板\": 3799, \"笔记本\": 5299},\n",
    "    {\"手机\": 6999, \"平板\": 4799, \"笔记本\": 6499},\n",
    "]\n",
    "# 构造DataFrame\n",
    "df7 = pd.DataFrame(list1)\n",
    "df7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "723addb6-0812-4649-b7b4-1fda0d050541",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-16T07:42:27.884720Z",
     "iopub.status.busy": "2022-04-16T07:42:27.884469Z",
     "iopub.status.idle": "2022-04-16T07:42:27.896876Z",
     "shell.execute_reply": "2022-04-16T07:42:27.896184Z",
     "shell.execute_reply.started": "2022-04-16T07:42:27.884694Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.615171</td>\n",
       "      <td>0.729912</td>\n",
       "      <td>0.392151</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.200781</td>\n",
       "      <td>0.667310</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.908103</td>\n",
       "      <td>0.364000</td>\n",
       "      <td>0.719871</td>\n",
       "      <td>0.352662</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         1         2         3\n",
       "0  0.615171  0.729912  0.392151       NaN\n",
       "1  0.200781  0.667310       NaN       NaN\n",
       "2  0.908103  0.364000  0.719871  0.352662"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3. Series构成的列表构造DataFrame\n",
    "# 构造列表\n",
    "list2 = [\n",
    "    pd.Series(np.random.rand(3)),\n",
    "    pd.Series(np.random.rand(2)),\n",
    "    pd.Series(np.random.rand(4)),\n",
    "]\n",
    "# 构造DataFrame\n",
    "df8 = pd.DataFrame(list2)\n",
    "df8"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51d6ebd9-a8c9-45dc-98c9-071f782f647a",
   "metadata": {},
   "source": [
    "## DataFrame基本用法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "07130547-5798-4d79-aff3-0e53dcb6f652",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-16T08:03:14.478829Z",
     "iopub.status.busy": "2022-04-16T08:03:14.478582Z",
     "iopub.status.idle": "2022-04-16T08:03:14.487488Z",
     "shell.execute_reply": "2022-04-16T08:03:14.486855Z",
     "shell.execute_reply.started": "2022-04-16T08:03:14.478803Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C   D\n",
       "a   0   1   2   3\n",
       "b   4   5   6   7\n",
       "c   8   9  10  11\n",
       "d  12  13  14  15"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建示例DataFrame\n",
    "df9 = pd.DataFrame(\n",
    "    np.arange(16).reshape(4, 4),\n",
    "    index=[\"a\", \"b\", \"c\", \"d\"],\n",
    "    columns=[\"A\", \"B\", \"C\", \"D\"],\n",
    ")\n",
    "df9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "7803e7bb-7d3d-402f-885c-72cfc573ab67",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-16T08:09:42.394369Z",
     "iopub.status.busy": "2022-04-16T08:09:42.393849Z",
     "iopub.status.idle": "2022-04-16T08:09:42.403283Z",
     "shell.execute_reply": "2022-04-16T08:09:42.402659Z",
     "shell.execute_reply.started": "2022-04-16T08:09:42.394336Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>8</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>9</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>3</td>\n",
       "      <td>7</td>\n",
       "      <td>11</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   a  b   c   d\n",
       "A  0  4   8  12\n",
       "B  1  5   9  13\n",
       "C  2  6  10  14\n",
       "D  3  7  11  15"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1. T转置\n",
    "df9.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "4fffaa0f-71b6-432a-abbc-165e42d528cc",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-16T08:24:12.341103Z",
     "iopub.status.busy": "2022-04-16T08:24:12.340815Z",
     "iopub.status.idle": "2022-04-16T08:24:12.346260Z",
     "shell.execute_reply": "2022-04-16T08:24:12.345561Z",
     "shell.execute_reply.started": "2022-04-16T08:24:12.341074Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a     2\n",
      "b     6\n",
      "c    10\n",
      "d    14\n",
      "Name: C, dtype: int64\n",
      "<class 'pandas.core.series.Series'>\n"
     ]
    }
   ],
   "source": [
    "# 2. 通过列索引获取列数据\n",
    "print(df9[\"C\"])\n",
    "print(type(df9[\"C\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "a4109ff5-ff58-49ef-b361-d769216bc984",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-16T08:27:43.317192Z",
     "iopub.status.busy": "2022-04-16T08:27:43.316939Z",
     "iopub.status.idle": "2022-04-16T08:27:43.325483Z",
     "shell.execute_reply": "2022-04-16T08:27:43.324871Z",
     "shell.execute_reply.started": "2022-04-16T08:27:43.317165Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C   D   E\n",
       "a   0   1   2   3  99\n",
       "b   4   5   6   7  99\n",
       "c   8   9  10  11  99\n",
       "d  12  13  14  15  99"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3. 增加数据列\n",
    "df9[\"E\"] = 99\n",
    "df9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "b4c8d0f9-da13-4c22-866e-5622f4fe28b4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-16T08:35:18.332813Z",
     "iopub.status.busy": "2022-04-16T08:35:18.332563Z",
     "iopub.status.idle": "2022-04-16T08:35:18.341130Z",
     "shell.execute_reply": "2022-04-16T08:35:18.340445Z",
     "shell.execute_reply.started": "2022-04-16T08:35:18.332788Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C   D  E\n",
       "a   0   1   2   3  2\n",
       "b   4   5   6   7  3\n",
       "c   8   9  10  11  4\n",
       "d  12  13  14  15  5"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3. 增加数据列\n",
    "df9[\"E\"] = [2, 3, 4, 5]\n",
    "df9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "dcadf8b2-729b-4a67-9b48-e8c756057234",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-16T08:41:37.008715Z",
     "iopub.status.busy": "2022-04-16T08:41:37.008472Z",
     "iopub.status.idle": "2022-04-16T08:41:37.016992Z",
     "shell.execute_reply": "2022-04-16T08:41:37.016245Z",
     "shell.execute_reply.started": "2022-04-16T08:41:37.008689Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C   D\n",
       "a   0   1   2   3\n",
       "b   4   5   6   7\n",
       "c   8   9  10  11\n",
       "d  12  13  14  15"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4. 删除列数据\n",
    "del(df9[\"E\"])\n",
    "df9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "006d5827-c8c8-4872-ac67-aa3741e4a486",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
